package com.example.ui.share;

import java.util.ArrayList;

import com.example.huashangapp.R;
import com.example.net.AnalyzingJson;
import com.example.net.NetRequest;
import com.example.ui.register.util.Province;
import com.example.ui.register.util.SelectProvinceAdapter;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class CircleSelectprovince extends Fragment {
	private ListView sc_list;

	private SelectProvinceAdapter sp_adapter;
	private ArrayList<Province> sp_list;
	private CreateCircleGroupActivity rf4;
	private String data;
	
//	private CreateCircleGroup ccg;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View v = inflater.inflate(R.layout.a_selectprovince, null);
		rf4 = (CreateCircleGroupActivity) getActivity();
		
		sc_list = (ListView) v.findViewById(R.id.as_list);
		new Thread(sp_run).start();
		sp_list = new ArrayList<Province>();
		sp_adapter = new SelectProvinceAdapter(v.getContext(), sp_list);

		sc_list.setAdapter(sp_adapter);
		sc_list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				// TODO Auto-generated method stub
				rf4.province = sp_list.get(position).getProvince();
				rf4.provinceID = sp_list.get(position).getProvinceID();

				CircleSelectCity csc = new CircleSelectCity();
				FragmentManager fm = getFragmentManager();
				FragmentTransaction ft = fm.beginTransaction();
				ft.replace(R.id.cc_rl, csc);
				ft.commit();
			}
		});
		return v;
	}

	private Handler h = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 1:

				sp_adapter.setCcInfos(sp_list);
				sp_adapter.notifyDataSetChanged();
				break;

			default:
				break;
			}

		};

	};
	Runnable sp_run = new Runnable() {

		@Override
		public void run() {
			// TODO Auto-generated method stub
			data = NetRequest.postRequestProvince();
			sp_list = AnalyzingJson.getcontent_province(data);

			h.sendEmptyMessage(1);
		}
	};

}
